/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.TorneoDao;
import entity.Torneo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 *
 * @author USER
 */
public class TorneoDaoImpl implements TorneoDao {

    private DriverManagerDataSource dataSource;

    public void setDataSource(DriverManagerDataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Integer insertar(Torneo Torneo) {
        Integer result = null;
        Double maxcod = 0.0;
        String conteo = "select max(codtor) from mkt_proyecto_Torneo";
        String sql = "";
        Connection cn = null;
        String fechaReg = Torneo.getFecreg().substring(8, 10) + "/" + Torneo.getFecreg().substring(3, 5) + "/" + Torneo.getFecreg().substring(0, 2);
        String fechaCie = Torneo.getFeccie().substring(8, 10) + "/" + Torneo.getFeccie().substring(3, 5) + "/" + Torneo.getFeccie().substring(0, 2);
        String fechaLan = Torneo.getFeclan().substring(8, 10) + "/" + Torneo.getFeclan().substring(3, 5) + "/" + Torneo.getFeclan().substring(0, 2);

        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(conteo);
            while (rs.next()) {
                maxcod = rs.getDouble(1);
            }
            maxcod = maxcod + 1;
            String maxnumber = maxcod.toString();
            sql = "insert into mkt_proyecto_Torneo(codtor,destor,urltor,tiptor,canpar,feclan,feccie,esttor,usureg,fecreg) values ('" + maxcod + "','" + Torneo.getDestor() + "','www.Torneo" + maxnumber.substring(0, 1) + ".com','" + Torneo.getTiptor() + "','" + Torneo.getCanpar() + "','" + fechaLan + "','" + fechaCie + "'," + 1.0 + ",'" + "Test1" + "','" + fechaReg + "')";
            int ctos = st.executeUpdate(sql);
            if (ctos == 0) {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    @Override
    public Integer asignar(Torneo Torneo, Double id, Integer pareja) {
        java.util.Date sysdate = new Date();
        SimpleDateFormat formatoDeFecha = new SimpleDateFormat("dd/MM/yyyy");
        String fechareg = formatoDeFecha.format(sysdate);
        String fechaReg = fechareg.substring(8, 10) + "/" + fechareg.substring(3, 5) + "/" + fechareg.substring(0, 2);
        Integer result = null;
        String sql = "";
        Connection cn = null;
        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            sql = "insert into mkt_proyecto_detalle(codtor,codcon,usureg,fecreg,pareja,ganador) values ('" + Torneo.getCodtor() + "','" + id + "','UserTest','" + fechaReg + "'," + pareja + ",0)";
            int ctos = st.executeUpdate(sql);
            if (ctos == 0) {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public void actualizar(Torneo Torneo) {
        String result = null;
        Connection cn = null;
        String fechaReg = Torneo.getFecreg().substring(8, 10) + "/" + Torneo.getFecreg().substring(3, 5) + "/" + Torneo.getFecreg().substring(0, 2);
        String fechaCie = Torneo.getFeccie().substring(8, 10) + "/" + Torneo.getFeccie().substring(3, 5) + "/" + Torneo.getFeccie().substring(0, 2);
        String fechaLan = Torneo.getFeclan().substring(8, 10) + "/" + Torneo.getFeclan().substring(3, 5) + "/" + Torneo.getFeclan().substring(0, 2);
        String sql = "update mkt_proyecto_Torneo set destor='" + Torneo.getDestor() + "', tiptor='" + Torneo.getTiptor() + "',canpar='" + Torneo.getCanpar() + "',feclan='" + fechaLan + "',feccie='" + fechaCie + "',esttor='" + 1.0 + "',fecreg='" + fechaReg + "' where codtor=" + Torneo.getCodtor();

        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            int ctos = st.executeUpdate(sql);
            if (ctos == 0) {
                result = "0 filas afectadas";
            }
        } catch (SQLException e) {
            result = e.getMessage();
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
                result = e.getMessage();
            }
        }
        System.out.println(result);
    }

    @Override
    public void eliminar(Double id) {
        String result = null;
        String sql = "delete from mkt_proyecto_Torneo where codtor=" + id;
        Connection cn = null;

        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            int ctos = st.executeUpdate(sql);
            if (ctos == 0) {
                result = "0 filas afectadas";
            }
        } catch (SQLException e) {
            result = e.getMessage();
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
                result = e.getMessage();
            }
        }
        System.out.println(result);
    }

 
    @Override
    public void lanzar(Double id) {
        java.util.Date sysdate = new Date();
        SimpleDateFormat formatoDeFecha = new SimpleDateFormat("dd/MM/yyyy");
        String fechareg = formatoDeFecha.format(sysdate);
        String fechaReg = fechareg.substring(8, 10) + "/" + fechareg.substring(3, 5) + "/" + fechareg.substring(0, 2);
        
        String result = null;
        String sql = "update mkt_proyecto_Torneo set esttor=2, feclan = '" + fechaReg + "' where codtor=" + id;
        Connection cn = null;

        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            int ctos = st.executeUpdate(sql);
            if (ctos == 0) {
                result = "0 filas afectadas";
            }
        } catch (SQLException e) {
            result = e.getMessage();
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
                result = e.getMessage();
            }
        }
        System.out.println(result);
    }    

    @Override
    public List<Torneo> buscarTodos() {
        List<Torneo> list = null;
        String sql = "select codtor,destor,urltor,tiptor,canpar,feclan,feccie,esttor,usureg,fecreg from mkt_proyecto_Torneo";
        Connection cn = null;

        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            list = new ArrayList<Torneo>();
            while (rs.next()) {
                Torneo Torneo = new Torneo();
                Torneo.setCodtor(rs.getDouble(1));
                Torneo.setDestor(rs.getString(2));
                Torneo.setUrltor(rs.getString(3));
                Torneo.setTiptor(rs.getString(4));
                Torneo.setCanpar(rs.getDouble(5));
                Torneo.setFeclan(rs.getString(6));
                Torneo.setFeccie(rs.getString(7));
                Torneo.setEsttor(rs.getDouble(8));
                Torneo.setUsureg(rs.getString(9));
                Torneo.setFecreg(rs.getString(10));
                list.add(Torneo);
            }
        } catch (SQLException e) {
            list = null;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public List<Torneo> buscar(Torneo Torneo) {
        List<Torneo> list = null;
        String sql = "select codtor,destor,urltor,tiptor,canpar,feclan,feccie,esttor,usureg,fecreg from mkt_proyecto_Torneo where destor like '%" + Torneo.getDestor() + "%'";
        Connection cn = null;
        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            list = new ArrayList<Torneo>();
            while (rs.next()) {
                Torneo Torneos = new Torneo();
                Torneos.setCodtor(rs.getDouble(1));
                Torneos.setDestor(rs.getString(2));
                Torneos.setUrltor(rs.getString(3));
                Torneos.setTiptor(rs.getString(4));
                Torneos.setCanpar(rs.getDouble(5));
                Torneos.setFeclan(rs.getString(6));
                Torneos.setFeccie(rs.getString(7));
                Torneos.setEsttor(rs.getDouble(8));
                Torneos.setUsureg(rs.getString(9));
                Torneos.setFecreg(rs.getString(10));
                list.add(Torneos);
            }
        } catch (SQLException e) {
            list = null;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return list;
    }

    @Override
    public Torneo buscar(Double id) {
        Torneo Torneo = null;
        String sql = "select codtor,destor,urltor,tiptor,canpar,feclan,feccie,esttor,usureg,fecreg from mkt_proyecto_Torneo where codtor =" + id;
        Connection cn = null;


        try {
            cn = dataSource.getConnection();
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                Torneo = new Torneo();
                Torneo.setCodtor(rs.getDouble(1));
                Torneo.setDestor(rs.getString(2));
                Torneo.setUrltor(rs.getString(3));
                Torneo.setTiptor(rs.getString(4));
                Torneo.setCanpar(rs.getDouble(5));
                Torneo.setFeclan(rs.getString(6));
                Torneo.setFeccie(rs.getString(7));
                Torneo.setEsttor(rs.getDouble(8));
                Torneo.setUsureg(rs.getString(9));
                Torneo.setFecreg(rs.getString(10));
            }
        } catch (SQLException e) {
            Torneo = null;
        } finally {
            try {
                cn.close();
            } catch (Exception e) {
            }
        }
        return Torneo;
    }

    @Override
    public List<Torneo> buscarPorNombre(String nombre) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
